package com.buba.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.mapper.PayrollmodelMapper;
import com.buba.mapper.SalaryitemMapper;
import com.buba.mapper.SalaryitemsMapper;
import com.buba.service.PayrollmodelService;
import entity.salary.Payrollmodel;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.salary.Salaryitem;
import entity.salary.Salaryitems;
import entity.salary.dto.insertPayModelAndSalaryitemDto;
import entity.salary.dto.salaryitemDto;
import entity.salary.vo.selAllPayrollModelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-14
 */
@Service
public class PayrollmodelServiceImpl extends ServiceImpl<PayrollmodelMapper, Payrollmodel> implements PayrollmodelService {

    @Resource
    private PayrollmodelMapper payrollmodelMapper;
    @Resource
    private SalaryitemsMapper salaryitemsMapper;
    @Resource
    private SalaryitemMapper salaryitemMapper;
    /* *
     * @Author: beisheng
     * @Descriprion: 根据创建时间倒序查询工资单模板
     * @Date: 2023/3/22 10:18
     * @Return: common.R
     * @Param: []
     */

    @Override
    public List<selAllPayrollModelVo> selAllPayrollModel() {
        List<selAllPayrollModelVo> selAllPayrollModelVos = new ArrayList<>();
        selAllPayrollModelVo selAllPayrollModelVo = new selAllPayrollModelVo();
        List<Payrollmodel> payrollmodels = payrollmodelMapper.selectList(new QueryWrapper<Payrollmodel>().eq("delete_flag", 0).orderByDesc("create_time"));
        for (Payrollmodel payrollmodel : payrollmodels) {
            BeanUtils.copyProperties(payrollmodel,selAllPayrollModelVo);
            Salaryitem salaryitem = salaryitemMapper.selectOne(new QueryWrapper<Salaryitem>().eq("delete_flag", 0).eq("xz_id", payrollmodel.getSchemeId()));
            selAllPayrollModelVo.setSalaryschemeName(salaryitem.getProjectName());
            selAllPayrollModelVos.add(selAllPayrollModelVo);
        }
        return selAllPayrollModelVos;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 更改工资单状态
     * @Date: 2023/3/22 10:38
     * @Return: common.R
     * @Param: [payrollId]
     */

    @Override
    public void updatePayStatus(Integer payrollId) {
        Payrollmodel payrollmodel = new Payrollmodel();
        payrollmodel.setPayrollStatus(1);
        payrollmodel.setUpdateTime(new Date());
        payrollmodelMapper.update(payrollmodel,new QueryWrapper<Payrollmodel>().eq("delete_flag",0).eq("sa_id",payrollId));
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 逻辑删除工资单模板并逻辑删除工资单模板关联薪资项目
     * @Date: 2023/3/22 13:32
     * @Return: common.R
     * @Param: [payrollId]
     */

    @Override
    public void delPayModelAndSalaryItem(Integer payrollId) {
        Payrollmodel payrollmodel = new Payrollmodel();
        payrollmodel.setDeleteFlag(1);
        payrollmodelMapper.update(payrollmodel,new QueryWrapper<Payrollmodel>().eq("delete_flag",0).eq("sa_id",payrollId));
        Salaryitems salaryitem = new Salaryitems();
        salaryitem.setDeleteFlag(1);
        salaryitemsMapper.update(salaryitem,new QueryWrapper<Salaryitems>().eq("delete_flag",0).eq("pa_id",payrollId));
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加工资单模板并添加工资单模板关联薪资项
     * @Date: 2023/3/22 18:40
     * @Return: common.R
     * @Param: [insertPayModelAndSalaryitemDto]
     */

    @Override
    public void insertPayModelAndSalaryitem(insertPayModelAndSalaryitemDto insertPayModelAndSalaryitemDto) {
        Payrollmodel payrollmodel = new Payrollmodel();
        BeanUtils.copyProperties(insertPayModelAndSalaryitemDto,payrollmodel);
        payrollmodel.setCreateTime(new Date());
        payrollmodel.setUpdateTime(new Date());
        payrollmodel.setDeleteFlag(0);
        payrollmodelMapper.insert(payrollmodel);
        List<salaryitemDto> salaryitemDtos = insertPayModelAndSalaryitemDto.getSalaryitemDtos();
        for (salaryitemDto salaryitemDto : salaryitemDtos) {
            Salaryitems salaryitems = new Salaryitems();
            BeanUtils.copyProperties(salaryitemDto,salaryitems);
            salaryitems.setPaId(payrollmodel.getSaId());
            salaryitems.setDeleteFlag(0);
            salaryitems.setCreateTime(new Date());
            salaryitems.setUpdateTime(new Date());
            salaryitemsMapper.insert(salaryitems);
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改工资单模板或修改工资单模板关联薪资项
     * @Date: 2023/3/22 20:26
     * @Return: common.R
     * @Param: [salaryitemDto]
     */

    @Override
    public void updatePayModelAndSalaryitem(insertPayModelAndSalaryitemDto salaryitemDto) {
        Payrollmodel payrollmodel = new Payrollmodel();
        BeanUtils.copyProperties(salaryitemDto,payrollmodel);
        payrollmodel.setUpdateTime(new Date());
        payrollmodelMapper.update(payrollmodel,new QueryWrapper<Payrollmodel>().eq("sa_id",salaryitemDto.getSaId()));
        List<salaryitemDto> salaryitemDtos = salaryitemDto.getSalaryitemDtos();
        for (salaryitemDto salaryitem : salaryitemDtos) {
            Salaryitems salaryitems = new Salaryitems();
            BeanUtils.copyProperties(salaryitem,salaryitems);
            salaryitems.setPaId(payrollmodel.getSaId());
            salaryitems.setDeleteFlag(0);
            salaryitems.setCreateTime(new Date());
            salaryitems.setUpdateTime(new Date());
            salaryitemsMapper.insert(salaryitems);
        }
    }
}
